package com.project.common.config;

import cn.dev33.satoken.stp.StpInterface;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限验证接口扩展
 *
 */
@Component
public class SaTokenLoadPerms implements StpInterface {
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 返回一个账号所拥有的权限码集合
     * List.of("*")上帝权限
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> list = redisTemplate.opsForList().range("user:p:" + loginId, 0, -1);
        if (null == list) {
            list = new ArrayList<>();
        }
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> list = redisTemplate.opsForList().range("user:r:" + loginId, 0, -1);
        if (null == list) {
            list = new ArrayList<>();
        }
        return list;
    }
}
